#include <iostream>
#include <sstream>

#include "xSuite/xSuite.hpp"

int main()
{
    xs::Renderer *rend = xs::create_engine(800, 600, false, L"Hello world!", false);
    xs::FontPtr font = rend->create_font(L"data/fonts/font.fnt");
    xs::SpritePtr sprite = rend->create_sprite(rend->get_texture(L"data/earth.png"));
    sprite->set_hot_spot(pos2(64,64));

    xs::Event event;
    xs::Timer timer;
    xs::Timer frame_timer;

    long accumulator = 0;
    int rotation = 0;
    int frame_count = 0;

    std::wstringstream fps;

    while(true)
    {
        if(rend->get_event(event))
        {
            if(event.type == xs::EventTypes::QUIT)
                break;
        }

        accumulator += timer.elapsed();
        timer.restart();

        if(accumulator > 1000) accumulator = 1000;

        while(accumulator>=10)
        {
            accumulator -= 10;
            rotation++;
        }

        frame_count++;
        if(frame_timer.elapsed() >= 1000)
        {
            frame_timer.restart();
            fps.str(L"");
            fps << L"FPS: " << frame_count << "; ft: "<<1.0f/frame_count<<L"; memory: "<<
                xs::GetMemoryUsage()/1024.0f<<"kB";
            frame_count = 0;
        }

        if(rend->begin_scene(xs::ARGB(0, 64, 128, 255)))
        {
            rend->set_projection(800, 600);
            rend->look_at(400, 300);
            
            font->print(5, 5, fps.str().c_str(), xs::ARGB(255,255,255,255));
            sprite->draw(400, 300, 0, 0, rotation);
            rend->end_scene();
        }
    }

    rend->drop();
}